package com.lin.missyou.manager.redis;

import com.lin.missyou.manager.rocketmq.ProducerSchedule;
import com.lin.missyou.manager.rocketmq.RocketMQConfiguration;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.Topic;
@ConditionalOnMissingBean(RocketMQConfiguration.class)
@Configuration
public class MessageListenerConfiguration {

    @Value("${spring.redis.listen-pattern}")
    public String pattern;


    /**
     * redis消息监听器容器
     * 可以添加多个监听不同话题的redis监听器，只需要把消息监听器和相应的消息订阅处理器绑定，该消息监听器
     * 通过反射技术调用消息订阅处理器的相关方法进行一些业务处理
     * @param redisConnectionFactory
     * @param topicMessageListener
     * @return
     */
    @Bean
    public RedisMessageListenerContainer listenerContainer(RedisConnectionFactory redisConnectionFactory,
                                                           TopicMessageListener topicMessageListener) {
        // 创建redis消息监听器容器
        RedisMessageListenerContainer listenerContainer = new RedisMessageListenerContainer();
        // 设置Redis 连接的线程安全工厂
        listenerContainer.setConnectionFactory(redisConnectionFactory);
        // 创建一个主题  （基于模式匹配）
        Topic topic = new PatternTopic(pattern);
        //订阅了一个主题
        listenerContainer.addMessageListener(topicMessageListener, topic);

        return listenerContainer;

    }
}
